// 测试vite-plugin-node-polyfills插件
import { authenticator } from 'otplib';

const MY_SECRET = 'vuixppgt3kx3fbtjp2pzgfzzccxqkv7q';

console.log('=== 测试vite-plugin-node-polyfills插件 ===');
console.log('Buffer是否可用:', typeof Buffer !== 'undefined');
console.log('process是否可用:', typeof process !== 'undefined');
console.log('crypto是否可用:', typeof crypto !== 'undefined');
console.log('globalThis.Buffer是否可用:', typeof globalThis.Buffer !== 'undefined');
console.log('globalThis.process是否可用:', typeof globalThis.process !== 'undefined');

function testPolyfillPlugin() {
  try {
    // 测试Buffer功能
    if (typeof Buffer !== 'undefined') {
      const testBuffer = Buffer.from('test', 'utf8');
      console.log('Buffer测试:', testBuffer.toString());
    }
    
    // 测试process功能
    if (typeof process !== 'undefined') {
      console.log('process.env.NODE_ENV:', process.env.NODE_ENV || 'undefined');
      console.log('process.version:', process.version || 'undefined');
    }
    
    // 测试crypto功能
    if (typeof crypto !== 'undefined' && crypto.createHmac) {
      const hmac = crypto.createHmac('sha1', 'test');
      hmac.update('data');
      const digest = hmac.digest('hex');
      console.log('crypto.createHmac测试:', digest);
    }
    
    // 测试otplib
    const code = authenticator.generate(MY_SECRET);
    console.log('生成的TOTP:', code);
    
    const remaining = authenticator.timeRemaining();
    console.log('剩余时间:', remaining, '秒');
    
    console.log('\n=== vite-plugin-node-polyfills插件测试成功！===');
    
  } catch (error) {
    console.error('测试失败:', error);
  }
}

testPolyfillPlugin();
